package lee.vioson.ebook.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import coil.compose.rememberImagePainter
import lee.vioson.ebook.R
import lee.vioson.ebook.data.picUrl
import lee.vioson.ebook.ui.AppTheme

/**
 *Author:viosonlee
 *Date:2023/6/17
 *DESCRIPTION:
 */
@Composable
fun VipScreen(
    navHostController: NavHostController,
) {
    Scaffold(topBar = {
        TopBar(
            title = stringResource(id = R.string.VIP),
            navHostController = navHostController
        )
    }) { paddingValues ->
        Column(
            Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .verticalScroll(rememberScrollState())
        ) {
            VipInfo()
            MemberSubscribe()
            Subscription()
            Subscription()
            Subscription()
            ServiceStatement()
            Benefits()
        }
    }
}

@Composable
fun VipInfo() {
    Row(
        Modifier
            .fillMaxWidth()
            .padding(horizontal = 10.dp, vertical = 16.dp)
    ) {
        Image(
            painter = rememberImagePainter(data = picUrl), contentDescription = "avatar",
            modifier = Modifier.size(64.dp)
        )
        Column(
            Modifier
                .weight(1f)
                .padding(horizontal = 12.dp),
            verticalArrangement = Arrangement.SpaceAround
        ) {
            Text(text = "email@email.com", fontSize = 14.sp, color = AppTheme.colors.onBackground)
            Row(verticalAlignment = Alignment.CenterVertically) {
                Image(
                    painter = painterResource(id = R.mipmap.icon_vip),
                    contentDescription = "vip icon",
                    modifier = Modifier
                        .padding(end = 5.dp)
                        .size(24.dp)
                )
                Text(text = "name", fontSize = 12.sp, color = AppTheme.appColors.grayTextColor)
            }
            Text(
                text = "Expiration time：January 27, 2020",
                fontSize = 12.sp,
                color = AppTheme.appColors.grayIconColor
            )
        }
    }
}

@Composable
fun MemberSubscribe() {
    Column(Modifier.fillMaxWidth()) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Box(
                modifier = Modifier
                    .height(30.dp)
                    .width(122.dp),
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(id = R.mipmap.ic_vip_members),
                    contentDescription = "MemberSubscribe",
                    modifier = Modifier
                        .height(30.dp)
                        .width(122.dp)
                )
                Text(
                    text = stringResource(id = R.string.Members),
                    fontSize = 18.sp,
                    color = AppTheme.appColors.blackTextColor,
                    fontWeight = FontWeight.SemiBold
                )
            }
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = stringResource(id = R.string.unsubscribe),
                fontSize = 14.sp,
                color = AppTheme.appColors.grayTextColor,
                modifier = Modifier
                    .clickable {
                        //unsubscribe
                    }
                    .padding(horizontal = 20.dp, vertical = 3.dp)
            )
        }
        Spacer(
            modifier = Modifier
                .height(2.dp)
                .fillMaxWidth()
                .background(color = AppTheme.appColors.vipColor)
        )
        Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.CenterEnd) {
            Text(
                text = stringResource(id = R.string.Membership_Service_Agreement),
                fontSize = 12.sp,
                color = AppTheme.appColors.grayTextColor,
                modifier = Modifier
                    .clickable {
                        //to Membership_Service_Agreement

                    }
                    .padding(horizontal = 16.dp, vertical = 14.dp)
            )
        }
    }
}

@Composable
fun Subscription() {
    Box(modifier = Modifier.padding(start = 16.dp, end = 16.dp, bottom = 10.dp)) {
        Row(
            Modifier
                .fillMaxWidth()
                .border(width = 1.dp, color = Color(0xFF56565B), shape = RoundedCornerShape(2.dp))
                .background(color = Color(0xFF22232B), shape = RoundedCornerShape(2.dp))
                .padding(horizontal = 16.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = stringResource(id = R.string.Yearly_subscription),
                fontSize = 14.sp,
                color = AppTheme.appColors.vipColor
            )
            Spacer(modifier = Modifier.weight(1f))
            Text(text = "\$59.9", fontSize = 30.sp, color = AppTheme.appColors.vipColor)
            Text(
                text = "\$240",
                fontSize = 14.sp,
                color = AppTheme.appColors.grayTextColor,
                modifier = Modifier.padding(start = 2.dp, end = 12.dp, top = 10.dp)
            )
            Button(
                colors = AppTheme.vipButtonColors(),
                shape = RoundedCornerShape(8.dp),
                onClick = { /*TODO*/ }) {
                Text(
                    text = stringResource(id = R.string.Buy),
                    fontSize = 18.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = AppTheme.appColors.blackTextColor
                )
            }
        }
    }
}

@Composable
fun ServiceStatement() {
    Column(
        Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
            .padding(bottom = 10.dp)
    ) {
        Text(
            text = stringResource(id = R.string.service_statement_title),
            fontSize = 14.sp,
            color = AppTheme.appColors.grayTextColor2,
            modifier = Modifier.padding(vertical = 10.dp)
        )
        Text(
            text = stringResource(id = R.string.service_statement_content),
            fontSize = 12.sp,
            color = AppTheme.appColors.grayTextColor2
        )
    }
}

@Composable
fun Benefits() {
    Column(Modifier.fillMaxWidth()) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Box(
                modifier = Modifier
                    .height(30.dp)
                    .width(122.dp),
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(id = R.mipmap.ic_vip_members),
                    contentDescription = "MemberSubscribe",
                    modifier = Modifier
                        .height(30.dp)
                        .width(122.dp)
                )
                Text(
                    text = stringResource(id = R.string.Benefits),
                    fontSize = 18.sp,
                    color = AppTheme.appColors.blackTextColor,
                    fontWeight = FontWeight.SemiBold
                )
            }
            Spacer(modifier = Modifier.weight(1f))

        }
        Spacer(
            modifier = Modifier
                .height(2.dp)
                .fillMaxWidth()
                .background(color = AppTheme.appColors.vipColor)
        )
        Column(Modifier.padding(20.dp)) {
            Text(
                text = stringResource(id = R.string.benefits_1), fontSize = 14.sp,
                color = AppTheme.appColors.grayTextColor2,
            )
            Spacer(modifier = Modifier.height(10.dp))
            Text(
                text = stringResource(id = R.string.benefits_2), fontSize = 14.sp,
                color = AppTheme.appColors.grayTextColor2,
            )
        }
    }
}

@Preview
@Composable
fun VipScreenPreview() {
    AppTheme {
        VipScreen(navHostController = rememberNavController())
    }
}